<template>
    <div class="lottery">
        <div class="lottery-item">
            <div class="lottery-start">
                <div class="box gray" v-if="isStart===0"><p>活动未开始</p></div>
                <div class="box" @click="startLottery" v-if="isStart===1"><p><b>抽奖</b></p></div>
                <div class="box gray" v-if="isStart===2"><p>活动已过期</p></div>
            </div>
            <ul>
                <li v-for="(item,i) in prizeList" :class="i==index?'on':''">
                    <div class="box">
                        <p v-if="item.prizeimg"><img :src="item.prizeimg" alt=""></p>
                        <p v-else><img src="http://hlj.offcn.com/zg/zgvipimg/left1.png" alt=""></p>
                        <p>{{item.prizename}}</p>
                    </div>
                </li>
            </ul>
        </div>

        <lotterymodal v-if="lotteryvisible" :visible.sync="lotteryvisible" :zjinfo="zjinfo"></lotterymodal>
    </div>
</template>

<script>
    import { UpdataScore } from '@/util/score'
    import lotterymodal from '@/components/modal/lotterymodal'
    export default {
        props: {
            prizeList: { type: Array, defaule: [] },
            actid: { type: Number, defaule: 0 },
        },
        name: "jiugongge",
        data() {
            return {
                isStart: 1,
                score: 10,
                index: -1, // 当前转动到哪个位置，起点位置
                count: 8, // 总共有多少个位置
                timer: 0, // 每次转动定时器
                speed: 100, // 初始转动速度
                times: 0, // 转动次数
                cycle: 50, // 转动基本次数：即至少需要转动多少次再进入抽奖环节
                prize: -1, // 中奖位置
                click: true,
                lotteryvisible: false,
                zjinfo: {},
            };
        },
        computed:{
            userdata(){
                return this.$store.getters.userdata ? this.$store.getters.userdata : ''
            },
        },
        created(){

        },
        methods: {
            choujiang(){
                var that = this
                if(!that.userdata){
                    that.$dialog.confirm({
                        title: '提示', message: '请先登录', confirmButtonText: '前往登陆', cancelButtonText: '取消'
                    }).then(() => {this.$router.push({ path: '/user/login' });}).catch(() => {});return
                }
                that.$http.jsonp('https://zg99.offcn.com/index/choujiang/lottery/?actid='+that.actid+'&callback=?', {
                    params: { name: that.userdata.name, phone: that.userdata.phone }
                })
                .then(function (res) {
                    var resjson = res.body
                    if(resjson.status==1){
                        that.zjinfo = that.prizeList.find((item,iti) => {
                            if(item.prizeid == resjson.prizeid) that.prize = iti
                            return item.prizeid == resjson.prizeid
                        })
                        this.startRoll();
                    }else{
                        that.$dialog.alert({
                            title: '提示',
                            message: resjson.msg,
                        });
                        that.click = true
                    }
                })
            },
            startLottery() {
                if (!this.click) return
                this.click = false
                this.choujiang()
            },
            startRoll() {
                this.times += 1 // 转动次数
                this.oneRoll()  // 转动过程调用的每一次转动方法，这里是第一次调用初始化
                // 如果当前转动次数达到要求 && 目前转到的位置是中奖位置
                if (this.times > this.cycle + 8 && this.prize === this.index) {
                    clearTimeout(this.timer)    // 清除转动定时器，停止转动
                    this.prize = -1
                    this.times = 0
                    this.speed = 200
                    this.click = true;
                    var that = this
                    setTimeout((res) => {
                        that.lotteryvisible = true
                        UpdataScore(that.zjinfo.prizescore)
                    },300)
                } else {
                    if (this.times < this.cycle) {
                        this.speed -= 10 // 加快转动速度
                    } else if (this.times > this.cycle + 10 && ((this.prize === 0 && this.index === 7) || this.prize === this.index + 1)) {
                        this.speed += 110
                    } else {
                        this.speed += 20
                    }
                    if (this.speed < 40) {
                        this.speed = 40
                    }
                    this.timer = setTimeout(this.startRoll, this.speed)
                }

            },
            oneRoll() {
                let index = this.index
                const count = this.count
                index += 1
                if (index > count - 1) {
                    index = 0
                }
                this.index = index
            },
        },
        components: {
            lotterymodal
        }
    };
</script>

<style scoped>
</style>